/*
 * Project Name: showtao
 * File Name: RESTSampleService.java
 * Class Name: RESTSampleService
 *
 * Copyright 2014 showtao.com
 *
 * Licensed under the showtao.com
 *
 * http://showtao.com
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.showtao.front.ws;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.showtao.front.domain.Product;
import com.showtao.front.service.ProductService;

/**
 * Class Name: RESTSampleService Description: TODO
 * 
 * @author chunlinwang
 * 
 */
@Path("/sample")
public class RESTSampleService {
    protected final Logger log = LoggerFactory.getLogger(RESTSampleService.class);

    @Autowired
    ProductService productService;

    @GET
    @Path("/getproduct/{productId}")
    @Produces({ "application/json" })
    // "application/xml",
    @Consumes({ "application/json" })
    public Response getProducts(@PathParam("productId") Long productId) throws Exception {
        if ("exception".equals(productId)) {
            throw new RuntimeException("Exception...");
        }

        Product result = productService.getOneJoinCategory(productId);

        if (result == null) {
            return Response.status(Response.Status.BAD_REQUEST).build();
        } else {
            return Response.ok(result).build();
        }

    }

    @GET
    @Path("/echo/{input}")
    @Produces("text/plain")
    public String ping(@PathParam("input") String input) {

        return input + ":in server!";

    }
}
